

using System;
using System.Collections.Generic;
using System.Text;
using DataAccessLayer;
using EntityLayer;
using DataLayer;
using System.Data;
using InfraStructureLayer;

namespace BusinessLayer
{
    public class busEntries : BusinessBase
    {
        public busEntries()
            : base()
        {
        }
        #region  Code Factory Generated Methods

        public DataTable SelectEntries()
        {
            try
            {
                datEntries insDatEntries = new datEntries();
                DbConnector insDbConnector = new DbConnector();
                return insDatEntries.SelectEntries(insDbConnector);
            }
            catch (SFException ex)
            {
                this.ErrorMessage = ex.Message;
            }
            return null;
        }
        public void SelectEntriesByEntryId(entEntries parEntEntries)
        {
            try
            {
                datEntries insDatEntries = new datEntries();
                DbConnector insDbConnector = new DbConnector();
                insDatEntries.SelectEntriesByEntryId(parEntEntries, insDbConnector);
            }
            catch (SFException ex)
            {
                this.ErrorMessage = ex.Message;
            }
        }

        #endregion
        public void DeleteEntriesByEntryId(entEntries parEntEntries)
        {
            DbConnector insDbConnector = new DbConnector();
            try
            {
                insDbConnector.BeginTransaction();

                datEntryCategories insDatEntryCategories = new datEntryCategories();
                entEntryCategories insEntEntryCategories = new entEntryCategories();
                insEntEntryCategories.EntryId = parEntEntries.EntryId;
                insDatEntryCategories.DeleteEntryCategoriesByEntryId(insEntEntryCategories, insDbConnector);

                datEntryContents insDatEntryContents = new datEntryContents();
                entEntryContents insEntEntryContents = new entEntryContents();
                insEntEntryContents.EntryId = parEntEntries.EntryId;
                DataTable insDataTableEntryContents = insDatEntryContents.SelectEntryContentsByEntryId(insEntEntryContents, insDbConnector);

                datEntryReferrers insDatEntryReferrers = new datEntryReferrers();
                datComments insDatComments = new datComments();
                foreach (DataRow dr in insDataTableEntryContents.Rows)
                {
                    if (dr["EntryContentId"] != DBNull.Value)
                    {
                        entEntryReferrers insEntEntryReferrers = new entEntryReferrers();
                        insEntEntryReferrers.EntryContentId = Convert.ToInt32(dr["EntryContentId"]);
                        insDatEntryReferrers.DeleteEntryReferrersByEntryContentId(insEntEntryReferrers, insDbConnector);

                        entComments insEntComments = new entComments();
                        insEntComments.EntryContentId = Convert.ToInt32(dr["EntryContentId"]);
                        insDatComments.DeleteCommentsByEntryContentId(insEntComments, insDbConnector);

                    }
                }
                insDatEntryContents.DeleteEntryContentsByEntryId(insEntEntryContents, insDbConnector);


                datEntries insDatEntries = new datEntries();
                insDatEntries.DeleteEntriesByEntryId(parEntEntries, insDbConnector);

                insDbConnector.CommitTransaction();
            }
            catch (SFException ex)
            {
                insDbConnector.RollbackTransaction();
                this.ErrorMessage = ex.Message;
            }
            catch (Exception ex)
            {
                insDbConnector.RollbackTransaction();
                throw new SFUnHandledException(ex);
            }
        }
        public void InsertEntries(entEntries parEntEntries)
        {
            DbConnector insDbConnector = new DbConnector();
            try
            {

                insDbConnector.BeginTransaction();
                datEntries insDatEntries = new datEntries();

                insDatEntries.InsertEntries(parEntEntries, insDbConnector);
                datEntryContents insDatEntryContents = new datEntryContents();

                datEntryCategories insDatEntryCategories = new datEntryCategories();

                foreach (entEntryCategories insEntEntryCategories in parEntEntries.EntryCategories)
                {
                    insEntEntryCategories.EntryId = parEntEntries.EntryId;
                    insDatEntryCategories.InsertEntryCategories(insEntEntryCategories, insDbConnector);
                }

                foreach (entEntryContents insEntEntryContents in parEntEntries.EntryContentsInsert)
                {
                    insEntEntryContents.EntryId = parEntEntries.EntryId;
                    insEntEntryContents.PermalinkGUID = Guid.NewGuid();
                    insDatEntryContents.InsertEntryContents(insEntEntryContents, insDbConnector);
                }
                foreach (entEntryContents insEntEntryContents in parEntEntries.EntryContentsUpdate)
                {
                    insEntEntryContents.EntryId = parEntEntries.EntryId;
                    insDatEntryContents.UpdateEntryContentsByEntryContentId(insEntEntryContents, insDbConnector);

                }
                foreach (entEntryContents insEntEntryContents in parEntEntries.EntryContentsDelete)
                {
                    insDatEntryContents.DeleteEntryContentsByEntryContentId(insEntEntryContents, insDbConnector);
                }
                insDbConnector.CommitTransaction();
            }
            catch (SFException ex)
            {
                insDbConnector.RollbackTransaction();
                this.ErrorMessage = ex.Message;
            }
            catch (Exception ex)
            {
                insDbConnector.RollbackTransaction();
                throw new SFUnHandledException(ex);
            }
        }
        public void UpdateEntriesByEntryId(entEntries parEntEntries)
        {

            DbConnector insDbConnector = new DbConnector();
            try
            {

                insDbConnector.BeginTransaction();
                datEntries insDatEntries = new datEntries();

                insDatEntries.UpdateEntriesByEntryId(parEntEntries, insDbConnector);
                datEntryContents insDatEntryContents = new datEntryContents();

                datEntryCategories insDatEntryCategories = new datEntryCategories();

                entEntryCategories insEntEntryCategoriesMain = new entEntryCategories();
                insEntEntryCategoriesMain.EntryId = parEntEntries.EntryId;

                insDatEntryCategories.DeleteEntryCategoriesByEntryId(insEntEntryCategoriesMain, insDbConnector);

                foreach (entEntryCategories insEntEntryCategories in parEntEntries.EntryCategories)
                {
                    insEntEntryCategories.EntryId = parEntEntries.EntryId;

                    insDatEntryCategories.InsertEntryCategories(insEntEntryCategories, insDbConnector);
                }

                foreach (entEntryContents insEntEntryContents in parEntEntries.EntryContentsInsert)
                {
                    insEntEntryContents.EntryId = parEntEntries.EntryId;
                    insEntEntryContents.PermalinkGUID = Guid.NewGuid();
                    insDatEntryContents.InsertEntryContents(insEntEntryContents, insDbConnector);
                }
                foreach (entEntryContents insEntEntryContents in parEntEntries.EntryContentsUpdate)
                {
                    insEntEntryContents.EntryId = parEntEntries.EntryId;
                    insDatEntryContents.UpdateEntryContentsByEntryContentId(insEntEntryContents, insDbConnector);

                }
                foreach (entEntryContents insEntEntryContents in parEntEntries.EntryContentsDelete)
                {
                    insDatEntryContents.DeleteEntryContentsByEntryContentId(insEntEntryContents, insDbConnector);
                }
                insDbConnector.CommitTransaction();
            }
            catch (SFException ex)
            {
                insDbConnector.RollbackTransaction();
                this.ErrorMessage = ex.Message;
            }
            catch (Exception ex)
            {
                insDbConnector.RollbackTransaction();
                throw new SFUnHandledException(ex);
            }
        }
        public DataTable SelectEntriesLatest()
        {
            try
            {
                datEntries insDatEntries = new datEntries();
                DbConnector insDbConnector = new DbConnector();
                return insDatEntries.SelectEntriesLatest(insDbConnector);

            }
            catch (SFException ex)
            {
                this.ErrorMessage = ex.Message;
            }
            return null;
        }
        public DataTable SelectEntryArchive()
        {
            try
            {
                datEntries insDatEntries = new datEntries();
                DbConnector insDbConnector = new DbConnector();
                DataTable insDataTableEntryArchive = insDatEntries.SelectEntryArchive(insDbConnector);
                insDataTableEntryArchive.Columns.Add("Value", typeof(string));
                insDataTableEntryArchive.Columns.Add("Text", typeof(string));
                foreach (DataRow dr in insDataTableEntryArchive.Rows)
                {
                    dr["Value"] = dr["EntryYear"].ToString() + (dr["EntryMonth"].ToString().Length == 1 ? "0" + dr["EntryMonth"].ToString() : dr["EntryMonth"].ToString());
                    dr["Text"] = GetErrorMessageMultilanguageString(30 + Convert.ToInt32(dr["EntryMonth"]), new object[] { }) + "," + dr["EntryYear"].ToString() + " (" + dr["EntryCount"].ToString() + ")";
                }
                return insDataTableEntryArchive;
            }
            catch (SFException ex)
            {
                this.ErrorMessage = ex.Message;
            }
            return null;
        }
        public DataTable SelectEntryDates()
        {
            try
            {
                datEntries insDatEntries = new datEntries();
                DbConnector insDbConnector = new DbConnector();
                return insDatEntries.SelectEntryDates(insDbConnector);

            }
            catch (SFException ex)
            {
                this.ErrorMessage = ex.Message;
            }
            return null;
        }
        public DataTable SelectEntriesBelongsToDates()
        {
            try
            {
                datEntries insDatEntries = new datEntries();
                DbConnector insDbConnector = new DbConnector();
                DataTable insDataTableEntries = insDatEntries.SelectEntriesBelongsToDates(insDbConnector);
                insDataTableEntries.Columns.Add("Categories", typeof(string));

                datEntryCategories insDatEntryCategories = new datEntryCategories();
                foreach (DataRow dr in insDataTableEntries.Rows)
                {
                    dr["Body"] = GenerateHandlerLink(dr["Body"].ToString());
                    entEntryCategories insEntEntryCategories = new entEntryCategories();
                    insEntEntryCategories.EntryId = Convert.ToInt32(dr["EntryId"]);
                    DataTable insDataTableEntryCategories = insDatEntryCategories.SelectEntryCategoriesByEntryIdAndLanguageId(insEntEntryCategories, insDbConnector);
                    string s = "";
                    foreach (DataRow drCat in insDataTableEntryCategories.Rows)
                    {
                        s += drCat["CategoryName"].ToString() + ",";
                    }
                    if (s.EndsWith(","))
                    {
                        s = s.Substring(0, s.Length - 1);
                    }
                    dr["Categories"] = s;
                }
                return insDataTableEntries;
            }
            catch (SFException ex)
            {
                this.ErrorMessage = ex.Message;
            }
            return null;
        }

        public DataTable SelectEntryDatesByCategoryId(int categoryId)
        {
            try
            {
                datEntries insDatEntries = new datEntries();
                DbConnector insDbConnector = new DbConnector();
                return insDatEntries.SelectEntryDatesByCategoryId(categoryId, insDbConnector);

            }
            catch (SFException ex)
            {
                this.ErrorMessage = ex.Message;
            }
            return null;
        }
        public DataTable SelectEntriesBelongsToDatesByCategoryId(int categoryId)
        {
            try
            {
                datEntries insDatEntries = new datEntries();
                DbConnector insDbConnector = new DbConnector();
                DataTable insDataTableEntries = insDatEntries.SelectEntriesBelongsToDatesByCategoryId(categoryId, insDbConnector);
                insDataTableEntries.Columns.Add("Categories", typeof(string));
                datEntryCategories insDatEntryCategories = new datEntryCategories();
                foreach (DataRow dr in insDataTableEntries.Rows)
                {
                    dr["Body"] = GenerateHandlerLink(dr["Body"].ToString());
                    entEntryCategories insEntEntryCategories = new entEntryCategories();
                    insEntEntryCategories.EntryId = Convert.ToInt32(dr["EntryId"]);
                    DataTable insDataTableEntryCategories = insDatEntryCategories.SelectEntryCategoriesByEntryIdAndLanguageId(insEntEntryCategories, insDbConnector);
                    string s = "";
                    foreach (DataRow drCat in insDataTableEntryCategories.Rows)
                    {
                        s += drCat["CategoryName"].ToString() + ",";
                    }
                    if (s.EndsWith(","))
                    {
                        s = s.Substring(0, s.Length - 1);
                    }
                    dr["Categories"] = s;
                }
                return insDataTableEntries;
            }
            catch (SFException ex)
            {
                this.ErrorMessage = ex.Message;
            }
            return null;
        }

        public DataTable SelectEntryDatesByPublishDate(entEntries parEntEntries)
        {
            try
            {
                datEntries insDatEntries = new datEntries();
                DbConnector insDbConnector = new DbConnector();
                return insDatEntries.SelectEntryDatesByPublishDate(parEntEntries, insDbConnector);

            }
            catch (SFException ex)
            {
                this.ErrorMessage = ex.Message;
            }
            return null;
        }
        public DataTable SelectEntriesBelongsToDatesByPublishDate(entEntries parEntEntries)
        {
            try
            {
                datEntries insDatEntries = new datEntries();
                DbConnector insDbConnector = new DbConnector();
                DataTable insDataTableEntries = insDatEntries.SelectEntriesBelongsToDatesByPublishDate(parEntEntries, insDbConnector);
                insDataTableEntries.Columns.Add("Categories", typeof(string));
                datEntryCategories insDatEntryCategories = new datEntryCategories();
                foreach (DataRow dr in insDataTableEntries.Rows)
                {
                    dr["Body"] = GenerateHandlerLink(dr["Body"].ToString());
                    entEntryCategories insEntEntryCategories = new entEntryCategories();
                    insEntEntryCategories.EntryId = Convert.ToInt32(dr["EntryId"]);
                    DataTable insDataTableEntryCategories = insDatEntryCategories.SelectEntryCategoriesByEntryIdAndLanguageId(insEntEntryCategories, insDbConnector);
                    string s = "";
                    foreach (DataRow drCat in insDataTableEntryCategories.Rows)
                    {
                        s += drCat["CategoryName"].ToString() + ",";
                    }
                    if (s.EndsWith(","))
                    {
                        s = s.Substring(0, s.Length - 1);
                    }
                    dr["Categories"] = s;
                }
                return insDataTableEntries;

            }
            catch (SFException ex)
            {
                this.ErrorMessage = ex.Message;
            }
            return null;
        }

        public DataTable SelectEntriesByLanguageId()
        {
            try
            {
                datEntries insDatEntries = new datEntries();
                DbConnector insDbConnector = new DbConnector();
                DataTable insDataTableEntries = insDatEntries.SelectEntriesByLanguageId(insDbConnector);
                insDataTableEntries.Columns.Add("Categories", typeof(string));
                datEntryCategories insDatEntryCategories = new datEntryCategories();
                foreach (DataRow dr in insDataTableEntries.Rows)
                {
                    dr["Body"] = GenerateHandlerLink(dr["Body"].ToString());
                    entEntryCategories insEntEntryCategories = new entEntryCategories();
                    insEntEntryCategories.EntryId = Convert.ToInt32(dr["EntryId"]);
                    DataTable insDataTableEntryCategories = insDatEntryCategories.SelectEntryCategoriesByEntryIdAndLanguageId(insEntEntryCategories, insDbConnector);
                    string s = "";
                    foreach (DataRow drCat in insDataTableEntryCategories.Rows)
                    {
                        s += drCat["CategoryName"].ToString() + ",";
                    }
                    if (s.EndsWith(","))
                    {
                        s = s.Substring(0, s.Length - 1);
                    }
                    dr["Categories"] = s;
                }
                return insDataTableEntries;
            }
            catch (SFException ex)
            {
                this.ErrorMessage = ex.Message;
            }
            return null;
        }

        public DataTable SelectEntriesLatestMainPage()
        {
            try
            {
                datEntries insDatEntries = new datEntries();
                DbConnector insDbConnector = new DbConnector();
                DataTable insDataTableEntries= insDatEntries.SelectEntriesLatestMainPage(insDbConnector);
                insDataTableEntries.Columns.Add("Categories", typeof(string));
                datEntryCategories insDatEntryCategories = new datEntryCategories();
                foreach (DataRow dr in insDataTableEntries.Rows)
                {
                    dr["Body"] = GenerateHandlerLink(dr["Body"].ToString());
                    entEntryCategories insEntEntryCategories = new entEntryCategories();
                    insEntEntryCategories.EntryId = Convert.ToInt32(dr["EntryId"]);
                    DataTable insDataTableEntryCategories = insDatEntryCategories.SelectEntryCategoriesByEntryIdAndLanguageId(insEntEntryCategories, insDbConnector);
                    string s = "";
                    foreach (DataRow drCat in insDataTableEntryCategories.Rows)
                    {
                        s += drCat["CategoryName"].ToString() + ",";
                    }
                    if (s.EndsWith(","))
                    {
                        s = s.Substring(0, s.Length - 1);
                    }
                    dr["Categories"] = s;
                }
                return insDataTableEntries;

            }
            catch (SFException ex)
            {
                this.ErrorMessage = ex.Message;
            }
            return null;
        }
        public DataTable SelectEntriesByDateMainPage(entEntries parEntEntries)
        {
            try
            {
                datEntries insDatEntries = new datEntries();
                DbConnector insDbConnector = new DbConnector();
                DataTable insDataTableEntries= insDatEntries.SelectEntriesByDateMainPage(parEntEntries, insDbConnector);
                insDataTableEntries.Columns.Add("Categories", typeof(string));
                datEntryCategories insDatEntryCategories = new datEntryCategories();
                foreach (DataRow dr in insDataTableEntries.Rows)
                {
                    dr["Body"] = GenerateHandlerLink(dr["Body"].ToString());
                    entEntryCategories insEntEntryCategories = new entEntryCategories();
                    insEntEntryCategories.EntryId = Convert.ToInt32(dr["EntryId"]);
                    DataTable insDataTableEntryCategories = insDatEntryCategories.SelectEntryCategoriesByEntryIdAndLanguageId(insEntEntryCategories, insDbConnector);
                    string s = "";
                    foreach (DataRow drCat in insDataTableEntryCategories.Rows)
                    {
                        s += drCat["CategoryName"].ToString() + ",";
                    }
                    if (s.EndsWith(","))
                    {
                        s = s.Substring(0, s.Length - 1);
                    }
                    dr["Categories"] = s;
                }
                return insDataTableEntries;

            }
            catch (SFException ex)
            {
                this.ErrorMessage = ex.Message;
            }
            return null;
        }
    }
}